package com.musiklegal;

import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class RegisterMenu extends Activity {
    /** Called when the activity is first created. */
	
	
	public void onResume()
	{
		super.onResume();
		
	}
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);
        
        
        Button register = (Button) this.findViewById(R.id.register);
        
        register.setOnClickListener(new OnClickListener() {
			
			public void onClick(View arg0) {
				if(MusikLegal.internet(getApplicationContext()))
				{
				tryReg();
				}
			else
			{
				Toast.makeText(getApplicationContext(), "You are unable to connect", Toast.LENGTH_SHORT).show();
            	}	
			
			}
		});
    }
  
    void tryReg()
    {
    	
    	EditText etxt_pass = (EditText) findViewById(R.id.pass);
		EditText etxt_user = (EditText) findViewById(R.id.user);
		EditText etxt_email = (EditText) findViewById(R.id.email);
		EditText etxt_name = (EditText) findViewById(R.id.name);
		
		String username = etxt_user.getText().toString();
		String password = etxt_pass.getText().toString();
    	String email = etxt_email.getText().toString();
    	String name = etxt_name.getText().toString();
    	
    	DefaultHttpClient client = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://secure.musiklegal.com/user/add");
        List< BasicNameValuePair > nvps = new ArrayList< BasicNameValuePair >();
        nvps.add(new BasicNameValuePair("USERNAME", username));
        nvps.add(new BasicNameValuePair("PASSWORD", password));
        nvps.add(new BasicNameValuePair("EMAIL", email));
        nvps.add(new BasicNameValuePair("NAME", name));
        nvps.add(new BasicNameValuePair("SECRET", "6bed384da48a9d67aa4eb86b04d95808"));
 
        try {   
      	  	UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(nvps,HTTP.UTF_8);
            
            httppost.setEntity(p_entity);
            
            HttpResponse response = client.execute(httppost);
            HttpEntity responseEntity = response.getEntity();
            

            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();
            XMLReader xr = sp.getXMLReader();
            
            LoginHandler myLoginHandler = new LoginHandler();
            xr.setContentHandler(myLoginHandler);
            xr.parse(retrieveInputStream(responseEntity));
            ParsedLoginDataSet parsedLoginDataSet = myLoginHandler.getParsedLoginData();
            

            
            if (parsedLoginDataSet.getExtractedString().equals("SUCCESS")) 
            {
            	
            	AlertDialog alertDialog = new AlertDialog.Builder(this).create();
        		alertDialog.setTitle("Success");
        		alertDialog.setMessage("Registration is complete");
        		alertDialog.setButton("OK", new DialogInterface.OnClickListener() { 
        			public void onClick(DialogInterface dialog, int which) {
        			Intent intent = new Intent(getApplicationContext(), MusikLegal.class);
                          startActivity(intent);
                          finish();
        			}
        		});
        		alertDialog.setIcon(R.drawable.oc2);
        		alertDialog.show();
              
             
            }
            else
            	{
            	AlertDialog alertDialog = new AlertDialog.Builder(this).create();
        		alertDialog.setTitle("Fail");
        		alertDialog.setMessage("Fail to register, invalid email or email already used");
        		alertDialog.setButton("OK", new DialogInterface.OnClickListener() { 
        			public void onClick(DialogInterface dialog, int which) {
        			Intent intent = new Intent(getApplicationContext(), MusikLegal.class);
                          startActivity(intent);
                          finish();
        			}
        		});
        		alertDialog.setIcon(R.drawable.oc2);
        		alertDialog.show();
            	
            	
            	}
      } catch (Exception e)
      {
          
      }
    
    
    
    }
    private InputSource retrieveInputStream(HttpEntity httpEntity) {
        InputSource insrc = null;
        try {
              insrc = new InputSource(httpEntity.getContent());
        } catch (Exception e) {
        }
        return insrc;
  } 

}